package cn.com.ghy.android.service;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.com.ghy.android.bean.CustomerInfoBean;
import cn.com.ghy.android.bean.LoginInfoBean;
import cn.com.ghy.android.bean.OrderDetailBean;
import cn.com.ghy.android.bean.OrderDetailBeanParser;
import cn.com.ghy.android.bean.OrderReportInfoBean;
import cn.com.ghy.android.bean.OrderReportInfoBeanParser;
import cn.com.ghy.android.bean.OrderSellCategoryBean;
import cn.com.ghy.android.bean.OrderSellCategoryBeanParser;
import cn.com.ghy.android.bean.OrderSellReportBean;
import cn.com.ghy.android.bean.OrderSellReportBeanParser;
import cn.com.ghy.android.bean.UnitInfoBean;
import cn.com.ghy.android.bean.UnitInfoBeanParser;
import cn.com.ghy.android.exception.HttpResponseNullException;
import cn.com.ghy.android.util.CommonStatic;
import cn.com.ghy.android.util.CommonUtil;

/**
 * @软件著作权:金红叶纸业集团有限公司
 * @系统名称:SFA系统
 * @Description:订单查询服务类
 * @author wuz  @date 2011-5-16 16:08:21
 * @版本历史:v1.0
 * @Copyright:Copyright (c) 2011-2012 
 */

public class OrderOpeService {
	private String url = CommonStatic.URL_HEAD + "OrderOpe_Ar.asmx";
	private String methodName = "";//GetUnit
	private Map<String, String> params = null;

	private static final String PARTNER = "partner";
	private static final String BISMT = "Bismt";
	private static final String ORDERHEAD = "orderhead";
	private static final String ORDERDETAIL = "orderdetail";
	private static final String ORDERSEARCH = "ordersearch";
	private static final String USERID = "userid";
	private static final String ORDERID = "orderid";
	private static final String SAOF = "saof";
	private static final String FAX = "fax";
	private static final String PERNR = "pernr";
	private static final String MONTHID = "monthid";

	public OrderOpeService() {
	}

	public List<UnitInfoBean> GetUnit() throws Exception {
		methodName = "GetUnit";
		String result = null;
		result = HttpService.callWS(url, methodName, params);
		byte[] buffer = org.kobjects.base64.Base64.decode(result);
		byte[] info = CommonUtil.decompressByteArray(buffer);
		InputStream in = new ByteArrayInputStream(info);
		List<UnitInfoBean> list = UnitInfoBeanParser.parseXML(in);
		in.close();
		CommonUtil.checkListNull(list);
		return list;
	}

	public List<OrderDetailBean> GetLastMatnr(String partner) throws Exception {
		methodName = "GetLastMatnr";
		params = new HashMap<String, String>();
		params.put(PARTNER, partner);
		String result = HttpService.callWS(url, methodName, params);
		byte[] buffer = org.kobjects.base64.Base64.decode(result);
		byte[] info = CommonUtil.decompressByteArray(buffer);
		InputStream in = new ByteArrayInputStream(info);
		List<OrderDetailBean> list = OrderDetailBeanParser.parseXML(in);
		in.close();
		CommonUtil.checkListNotNull(list);
		return list;
	}

	public OrderDetailBean GetMatnr(String bismt) throws Exception {
		methodName = "GetMatnr";
		params = new HashMap<String, String>();
		params.put(BISMT, bismt);
		String result = HttpService.callWS(url, methodName, params);
		if (null != result) {
			byte[] buffer = org.kobjects.base64.Base64.decode(result);
			byte[] info = CommonUtil.decompressByteArray(buffer);
			InputStream in = new ByteArrayInputStream(info);
			List<OrderDetailBean> list = OrderDetailBeanParser.parseXML(in);
			in.close();
			if (null != list && list.size() > 0) {
				return list.get(0);
			}
		}
		return null;
	}

	public String OrderAdd(CustomerInfoBean customerInfo, String orderno, String xqriqi,
			List<OrderDetailBean> list, LoginInfoBean loginInfo) throws Exception {
		methodName = "OrderAdd";
		params = new HashMap<String, String>();

		StringWriter writer = new StringWriter();
		String orderheadXML = OrderDetailBeanParser.createXMLOrderhead(customerInfo, orderno,
				xqriqi, loginInfo, writer);
		writer.close();
		writer = new StringWriter();
		String orderdetailXML = OrderDetailBeanParser.createXMLOrderdetail(list, writer);
		writer.close();
		orderheadXML = org.kobjects.base64.Base64.encode(CommonUtil.compressByteArray(orderheadXML
				.getBytes()));
		orderdetailXML = org.kobjects.base64.Base64.encode(CommonUtil
				.compressByteArray(orderdetailXML.getBytes()));

		params.put(ORDERHEAD, orderheadXML);
		params.put(ORDERDETAIL, orderdetailXML);

		String result = HttpService.callWS(url, methodName, params);

		return result;
	}

	public String OrderEdit(CustomerInfoBean customerInfo, String orderno, String xqriqi,
			List<OrderDetailBean> list, LoginInfoBean loginInfo) throws Exception {
		methodName = "OrderEdit";
		params = new HashMap<String, String>();

		StringWriter writer = new StringWriter();
		String orderheadXML = OrderDetailBeanParser.createXMLOrderhead(customerInfo, orderno,
				xqriqi, loginInfo, writer);
		writer.close();
		writer = new StringWriter();
		String orderdetailXML = OrderDetailBeanParser.createXMLOrderdetail(list, writer);
		writer.close();
		orderheadXML = org.kobjects.base64.Base64.encode(CommonUtil.compressByteArray(orderheadXML
				.getBytes()));
		orderdetailXML = org.kobjects.base64.Base64.encode(CommonUtil
				.compressByteArray(orderdetailXML.getBytes()));

		params.put(ORDERHEAD, orderheadXML);
		params.put(ORDERDETAIL, orderdetailXML);

		String result = HttpService.callWS(url, methodName, params);

		return result;
	}

	public List<OrderReportInfoBean> GetOrderList(OrderReportInfoBean orderReportInfo,
			LoginInfoBean loginInfo) throws Exception {
		methodName = "GetOrderList";
		params = new HashMap<String, String>();

		List<OrderReportInfoBean> list = null;

		StringWriter writer = new StringWriter();
		String dsXML = OrderReportInfoBeanParser.createXMLOrderReportInfoQuery(orderReportInfo,
				writer);
		writer.close();
		dsXML = org.kobjects.base64.Base64.encode(CommonUtil.compressByteArray(dsXML.getBytes()));

		params.put(ORDERSEARCH, dsXML);
		params.put(USERID, loginInfo.getPernr());

		String result = HttpService.callWS(url, methodName, params);
		byte[] buffer = org.kobjects.base64.Base64.decode(result);
		byte[] info = CommonUtil.decompressByteArray(buffer);
		InputStream in = new ByteArrayInputStream(info);
		list = OrderReportInfoBeanParser.parseXMLForOrderReportInfo(in);
		in.close();
		CommonUtil.checkListNotNull(list);
		return list;
	}

	public Map<String, Object> OrderShow(String orderid) throws Exception {
		methodName = "OrderShow";
		params = new HashMap<String, String>();
		params.put(ORDERID, orderid);
		String result = HttpService.callWS(url, methodName, params);
		byte[] buffer = org.kobjects.base64.Base64.decode(result);
		byte[] info = CommonUtil.decompressByteArray(buffer);
		InputStream in = new ByteArrayInputStream(info);
		CustomerInfoBean customerInfoBean = OrderReportInfoBeanParser
				.parseXMLForCustomerInfoBean(in);
		in.close();
		in = new ByteArrayInputStream(info);
		List<OrderDetailBean> orderDetailList = OrderReportInfoBeanParser
				.parseXMLForOrderDetailBean(in);
		CommonUtil.checkListNotNull(orderDetailList);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("customerInfoBean", customerInfoBean);
		map.put("orderDetailList", orderDetailList);
		in.close();
		if (map.size() > 0) {
			return map;
		}
		return null;
	}

	public List<String[]> RpSaleList(String monthid, LoginInfoBean loginInfo) throws Exception {

		methodName = "RpSaleList";
		params = new HashMap<String, String>();
		List<String[]> list = null;

		StringWriter writer = new StringWriter();
		String orderheadXML = OrderSellReportBeanParser.createXMLOrderShellReportQuery(monthid,
				writer);
		writer.close();
		orderheadXML = org.kobjects.base64.Base64.encode(CommonUtil.compressByteArray(orderheadXML
				.getBytes()));

		params.put(ORDERHEAD, orderheadXML);
		params.put(USERID, loginInfo.getPernr());
		params.put(SAOF, loginInfo.getSaof());
		String result = "";
		try {
			result = HttpService.callWS(url, methodName, params);
		} catch (HttpResponseNullException hrne) {
			if (hrne.getMsg() == null) {
				throw new HttpResponseNullException();
			}
			result = "";
		}

		if (!"".equals(result)) {
			byte[] buffer = org.kobjects.base64.Base64.decode(result);
			byte[] info = CommonUtil.decompressByteArray(buffer);
			InputStream in = new ByteArrayInputStream(info);
			OrderSellReportBean bean = OrderSellReportBeanParser.parseXML(in);
			in.close();
			if (null != bean) {
				list = new ArrayList<String[]>();
				String[] value = new String[] {
						"工号",
						null == bean.getLoginInfoBean().getPernr() ? "" : bean.getLoginInfoBean()
								.getPernr() };
				list.add(value);
				value = new String[] {
						"姓名",
						null == bean.getLoginInfoBean().getDspname() ? "" : bean.getLoginInfoBean()
								.getDspname() };
				list.add(value);
				value = new String[] { "属性",
						null == bean.getSaletypename() ? "" : bean.getSaletypename() };
				list.add(value);
				value = new String[] { "客户数",
						null == bean.getPartnernum() ? "" : bean.getPartnernum() };
				list.add(value);
				value = new String[] { "订单张数", null == bean.getOrdernum() ? "" : bean.getOrdernum() };
				list.add(value);
				value = new String[] { "订单箱数", null == bean.getTotalnum() ? "" : bean.getTotalnum() };
				list.add(value);
				value = new String[] { "订单未税金额",
						null == bean.getTotalmoney() ? "" : bean.getTotalmoney() };
				list.add(value);
				value = new String[] { "订单NRT金额",
						null == bean.getTotalfpmoney() ? "" : bean.getTotalfpmoney() };
				list.add(value);
				value = new String[] { "订单NRT占比",
						null == bean.getPerordernrt() ? "" : bean.getPerordernrt() };
				list.add(value);
				value = new String[] { "送货张数",
						null == bean.getSendorder() ? "" : bean.getSendorder() };
				list.add(value);
				value = new String[] { "送货数量", null == bean.getSendnum() ? "" : bean.getSendnum() };
				list.add(value);
				value = new String[] { "送货未税金额",
						null == bean.getSendmoney() ? "" : bean.getSendmoney() };
				list.add(value);
				value = new String[] { "送货NRT金额",
						null == bean.getSendfpmoney() ? "" : bean.getSendfpmoney() };
				list.add(value);
				value = new String[] { "送货NRT占比",
						null == bean.getPersendnrt() ? "" : bean.getPersendnrt() };
				list.add(value);
				value = new String[] { "箱数满足率", null == bean.getPernum() ? "" : bean.getPernum() };
				list.add(value);
				value = new String[] { "金额满足率",
						null == bean.getPermoney() ? "" : bean.getPermoney() };
				list.add(value);
				value = new String[] { "月销额目标",
						null == bean.getSalemoney() ? "" : bean.getSalemoney() };
				list.add(value);
				value = new String[] { "月NRT目标",
						null == bean.getSalefpmoney() ? "" : bean.getSalefpmoney() };
				list.add(value);
				value = new String[] { "销额达成率", null == bean.getPersale() ? "" : bean.getPersale() };
				list.add(value);
				value = new String[] { "NRT达成率",
						null == bean.getPerfpsale() ? "" : bean.getPerfpsale() };
				list.add(value);
			}
		}
		return list;
	}

	public List<String[]> RpOrderPl(String monthid, LoginInfoBean loginInfo) throws Exception {

		methodName = "RpOrderPl";
		params = new HashMap<String, String>();
		List<String[]> listStr = null;

		params.put(MONTHID, monthid);
		params.put(SAOF, loginInfo.getSaof());
		params.put(PERNR, loginInfo.getPernr());

		String result = "";
		try {
			result = HttpService.callWS(url, methodName, params);
		} catch (HttpResponseNullException hrne) {
			if (hrne.getMsg() == null) {
				throw new HttpResponseNullException();
			}
			result = "";
		}

		if (!"".equals(result)) {
			byte[] buffer = org.kobjects.base64.Base64.decode(result);
			byte[] info = CommonUtil.decompressByteArray(buffer);
			InputStream in = new ByteArrayInputStream(info);
			List<OrderSellCategoryBean> list = OrderSellCategoryBeanParser.parseXML(in);
			in.close();
			if (null != list && list.size() > 0) {
				OrderSellCategoryBean bean = list.get(0);
				listStr = new ArrayList<String[]>();
				String[] value = new String[] { "RT", null == bean.getRT() ? "" : bean.getRT() };
				listStr.add(value);
				value = new String[] { "CRT", null == bean.getCRT() ? "" : bean.getCRT() };
				listStr.add(value);
				value = new String[] { "JRT", null == bean.getJRT() ? "" : bean.getJRT() };
				listStr.add(value);
				value = new String[] { "IT", null == bean.getIT() ? "" : bean.getIT() };
				listStr.add(value);
				value = new String[] { "BF", null == bean.getBF() ? "" : bean.getBF() };
				listStr.add(value);
				value = new String[] { "IF", null == bean.getIF() ? "" : bean.getIF() };
				listStr.add(value);
				value = new String[] { "PF", null == bean.getPF() ? "" : bean.getPF() };
				listStr.add(value);
				value = new String[] { "HKS", null == bean.getHKS() ? "" : bean.getHKS() };
				listStr.add(value);
				value = new String[] { "HKM", null == bean.getHKM() ? "" : bean.getHKM() };
				listStr.add(value);
				value = new String[] { "NK", null == bean.getNK() ? "" : bean.getNK() };
				listStr.add(value);
				value = new String[] { "FT", null == bean.getFT() ? "" : bean.getFT() };
				listStr.add(value);
				value = new String[] { "HT", null == bean.getHT() ? "" : bean.getHT() };
				listStr.add(value);
				value = new String[] { "KT", null == bean.getKT() ? "" : bean.getKT() };
				listStr.add(value);
				value = new String[] { "WT", null == bean.getWT() ? "" : bean.getWT() };
				listStr.add(value);
				value = new String[] { "小计",
						null == bean.getTOTALSENDMONEY() ? "" : bean.getTOTALSENDMONEY() };
				listStr.add(value);
			}
		}
		return listStr;
	}

	public String OrderFaxSend(String orderid, String fax, LoginInfoBean loginInfo)
			throws Exception {
		methodName = "OrderFaxSend";
		params = new HashMap<String, String>();
		params.put(ORDERID, orderid);
		params.put(FAX, fax);
		params.put(PERNR, loginInfo.getPernr());
		String result = HttpService.callWS(url, methodName, params);
		return result;
	}
}
